<?php
namespace Admin\Model;

use Common\Model\CommonModel;

class SaleModel extends CommonModel {
	protected $softdelete = true;
	protected $deleteTime = 'delete_time';

	public function get_one($sale_id){
	    $sale = M("Sale")->field('s.*, sm.sale_manage_name, sm.balance as upper_balance, sm.give_percent')->alias('s')->join("bdt_sale_manager sm ON sm.id = s.sale_manager_id","LEFT")->where(array("s.id"=>$sale_id))->find();
	    return $sale;
	}


    public function get_list($where = array(),$order = 'id'){
        $list = M("Sale")->field("id,sale_name,telphone,cover,address,lng,lat")->where($where)
            ->order($order)
            ->select();

        foreach($list AS &$litem){
            $good_list = D("Sale/SaleGood")->field("good_name,good_unit_price,vip_good_unit_price,good_unit")->where("sale_id=".$litem['id'])->select();
            $litem['good_list'] = $good_list;
            $litem['cover'] = $litem['cover']?sp_get_image_url($litem['cover']):'';
            $litem['telphone'] = $litem['telphone']?($litem['telphone']):'';
            $litem['address'] = $litem['address']?($litem['address']):'';
            $litem['lng'] = $litem['lng']?($litem['lng']):'';
            $litem['lat'] = $litem['lat']?($litem['lat']):'';
            $litem['gps'] = ($litem['lng'] && $litem['lat'])?'{'.$litem['lng'].','.$litem['lat'].'}':'{}';
        }
        return $list;
    }
}